package com.sample.dubbo.demo.api;

import com.sample.dubbo.demo.dto.Customer;
import com.sample.dubbo.demo.dto.QuerySchema;
import com.sample.dubbo.demo.exceptions.ArgumentException;

import java.util.List;

public class CustomerServiceStub implements CustomerService {
    CustomerService proxy;


    public CustomerServiceStub(CustomerService proxy) {
        this.proxy = proxy;
    }

    @Override
    public Customer save(Customer customer) {
        if (customer == null) {
            throw new ArgumentException("customer_required", "未传入客户");
        }
        if (isEmpty(customer.getCode())) {
            throw new ArgumentException("customer_code_required", "未传入客户编码");
        }
        if (isEmpty(customer.getName())) {
            throw new ArgumentException("customer_name_required", "未传入客户名称");
        }
        if (isEmpty(customer.getPhone())) {
            throw new ArgumentException("customer_phone_required", "未传入客户手机号码");
        }
        return proxy.save(customer);
    }

    @Override
    public List<Customer> query(QuerySchema querySchema) {
        if (querySchema == null || isEmpty(querySchema.getCondition())) {
            throw new ArgumentException("condition_required", "未传入查询条件");
        }
        return proxy.query(querySchema);
    }

    @Override
    public Customer find(Long id) {
        if (id == null || id == 0) {
            throw new ArgumentException("id_required", "未传入参数id");
        }
        return proxy.find(id);
    }

    @Override
    public boolean delete(Long id) {
        if (id == null || id == 0) {
            throw new ArgumentException("id_required", "未传入参数id");
        }
        return proxy.delete(id);
    }

    private boolean isEmpty(String str) {
        if (str == null || str.length() == 0) {
            return true;
        }
        return false;
    }
}